package com.hxh.test;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/9/6 19:37
 * synchronized 关键字复习
 */
public class Demo02 {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "BB").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "CC").start();
    }
}

// 资源类
class Ticket {
    // 票数
    private int num = 30;

    // 卖票
    public synchronized void sale() {
        if (num > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了第" + (num--) + "张票, 当前剩余票数为: " + num);
        }
    }
}
